package com.wise.site.utils;

import com.tencent.weibo.api.TAPI;
import com.tencent.weibo.constants.OAuthConstants;
import com.tencent.weibo.oauthv2.OAuthV2;
import com.tencent.weibo.webview.OAuthV2AuthorizeWebView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class QQWeiboUtil {
	
	public final static int RESULT = 1000;
	
	public static boolean checkAuthorize(Context context){
		boolean isAuthorize = false;
		DBOpenHelper dbOpenHelper = new DBOpenHelper(context);
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.query("oauthKey", new String[]{"weibotype", "userName", "token", "openId", "clientId"}, "weibotype = ?", new String[]{"qq"}, null, null, null);
		if(cursor.moveToNext()){
			isAuthorize = true;
		}
		cursor.close();
		db.close();
		dbOpenHelper.close();
		/*SharedPreferences userInfo = context.getSharedPreferences("user_info", Activity.MODE_WORLD_WRITEABLE);
		String token = userInfo.getString("token", "");
		String openId = userInfo.getString("openId", "");
		String clientId = userInfo.getString("clientId", "");
		if("".equals(token) || "".equals(openId) || "".equals(clientId)){
			
		}else{
			isAuthorize = true;
		}*/
		return isAuthorize;
	}
	
	public static void callAuthorizeUI(Activity activity){
		OAuthV2 oauth = new OAuthV2();
    	oauth.setRedirectUri("http://www.google.com.hk");
    	oauth.setClientId("801239315");
    	oauth.setClientSecret("ab6c77b0777e62bbe579444aa588d721");
    	Intent intent = new Intent(activity.getApplicationContext(), OAuthV2AuthorizeWebView.class);
    	intent.putExtra("oauth", oauth);
    	activity.startActivityForResult(intent, RESULT);
	}
	
	public static int sendQQWeibo(Context context, String content){
		int state = -1;
		OAuthV2 oauth = new OAuthV2();
		TAPI tApi = new TAPI(OAuthConstants.OAUTH_VERSION_2_A);
		DBOpenHelper dbOpenHelper = new DBOpenHelper(context);
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.query("oauthKey", new String[]{"token", "openId", "clientId"}, "weibotype = ?", new String[]{"qq"}, null, null, null);
		if(cursor.moveToNext()){
			String token = cursor.getString(cursor.getColumnIndex("token"));
			String openId = cursor.getString(cursor.getColumnIndex("openId"));
			String clientId = cursor.getString(cursor.getColumnIndex("clientId"));
			oauth.setAccessToken(token);
			oauth.setOpenid(openId);
			oauth.setClientId(clientId);
		}
		cursor.close();
		db.close();
		dbOpenHelper.close();
		/*SharedPreferences userInfo = context.getSharedPreferences("user_info", Activity.MODE_WORLD_WRITEABLE);
		oauth.setAccessToken(userInfo.getString("token", ""));
		oauth.setOpenid(userInfo.getString("openId", ""));
		oauth.setClientId(userInfo.getString("clientId", ""));*/
		try {
			tApi.add(oauth, "json", content, "127.0.0.1");
			state = oauth.getStatus();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return state;
	}
	
	public static boolean authorize(Context context, String userName, String token, String openId, String clientId){
		boolean isSuccess = false;
		try {
			logOut(context);
			DBOpenHelper dbOpenHelper = new DBOpenHelper(context);
			SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
			db.execSQL("insert into oauthKey(weibotype, userName, token, openId, clientId) values(?, ?, ?, ?, ?)", new Object[]{"qq", userName, token, openId, clientId});
			db.close();
			dbOpenHelper.close();
			
			/*SharedPreferences userInfo = context.getSharedPreferences("user_info", Activity.MODE_WORLD_WRITEABLE);
			SharedPreferences.Editor editor = userInfo.edit();
			editor.putString("token", token);
			editor.putString("openId", openId);
			editor.putString("clientId", clientId);
			editor.commit();*/
			
			isSuccess = true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return isSuccess;
	}
	
	public static boolean logOut(Context context){
		boolean falg = false;
		try {
			DBOpenHelper dbOpenHelper = new DBOpenHelper(context);
			SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
			db.execSQL("delete from oauthKey where weibotype = ?", new String[]{"qq"});
			db.close();
			dbOpenHelper.close();
			falg = true;
		} catch (Exception e) {
		}
		return falg;
	}
	
	
	public static String getAuthorizeUserName(Context context){
		String userName = null;
		DBOpenHelper dbOpenHelper = new DBOpenHelper(context);
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		Cursor cursor = db.query("oauthKey", new String[]{"weibotype", "userName", "token", "openId", "clientId"}, "weibotype = ?", new String[]{"qq"}, null, null, null);
		if(cursor.moveToNext()){
			userName = cursor.getString(cursor.getColumnIndex("userName"));
		}
		cursor.close();
		db.close();
		dbOpenHelper.close();
		return userName;
	}

}
